﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using Fujitsu.JiraAnalysis.Common;

namespace Fujitsu.JiraAnalysis.DAL
{
    public class FixVersionStorage
    {
        public IEventLog eventLog = new EventLogWrapper();

        public int? CheckAndAdd(int issueId, string jiraVersionId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var dbVersion = db.Version.Where(v => v.JiraVersionID == jiraVersionId).First();
                var fixVersion = (from f in db.FixVersion
                                  where f.IssueID == issueId && f.VersionID == dbVersion.ID
                                  select f).FirstOrDefault();
                if (fixVersion == null)
                {
                    fixVersion = new FixVersion()
                    {
                        IssueID = issueId,
                        VersionID = dbVersion.ID
                    };
                    try
                    {
                        db.AddToFixVersion(fixVersion);
                        db.SaveChanges();

                        return fixVersion.VersionID;
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        db.Refresh(RefreshMode.ClientWins, fixVersion);
                        db.SaveChanges();
                    }
                    catch (DataException ex)
                    {
                        EventLogger.CreateInstance(eventLog).LogWriteError(string.Concat("Can't add version to DB.", Environment.NewLine, ex.StackTrace));
                        return null;
                    }
                }
                return null;
            }
        }

        public void DeleteFixVersion(int issueId, int versionId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var dbFixVersion = db.FixVersion.FirstOrDefault(v => v.IssueID == issueId && v.VersionID == versionId);
                if (dbFixVersion != null)
                {
                    try
                    {
                        db.FixVersion.DeleteObject(dbFixVersion);
                        db.SaveChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        db.Refresh(RefreshMode.ClientWins, dbFixVersion);
                        db.SaveChanges();
                    }
                    catch (DataException ex)
                    {
                        EventLogger.CreateInstance(eventLog).LogWriteError(string.Concat("Can't delete fixversion from DB.", Environment.NewLine, ex.StackTrace));
                    }
                }
            }
        }

        public List<Version> GetFixVersionsOfIssue(int issueId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return (from i in db.FixVersion
                        where i.IssueID == issueId
                        select i.Version).ToList<Version>();
            }
        }
    }
}
